package com.streamer.app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.streamer.app.R
import com.streamer.app.bean.Item

/**
 * 本地播放文件列表适配器
 */
class NetWorkPlayAdapter(
    private val fileList: MutableList<Item>,
    private val onItemAction: (Item, String) -> Unit
) : RecyclerView.Adapter<NetWorkPlayAdapter.LocalPlayViewHolder>() {

    private var selectedPosition = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocalPlayViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_local_play, parent, false)
        return LocalPlayViewHolder(view)
    }

    override fun onBindViewHolder(holder: LocalPlayViewHolder, position: Int) {
        val fileName = fileList[position]
        holder.bind(fileName, position == selectedPosition)
    }

    override fun getItemCount(): Int = fileList.size

    /**
     * 更新数据
     */
    fun updateData(newList: List<Item>) {
        fileList.clear()
        fileList.addAll(newList)
        notifyDataSetChanged()
    }

    /**
     * 设置选中项
     */
    fun setSelectedItem(fileName: Item) {
        val newPosition = fileList.indexOf(fileName)
        if (newPosition != -1) {
            val oldPosition = selectedPosition
            selectedPosition = newPosition
            
            if (oldPosition != -1) {
                notifyItemChanged(oldPosition)
            }
            notifyItemChanged(selectedPosition)
        }
    }


    /**
     * 删除文件
     */
    fun removeFile(position: Int) {
        if (position in 0 until fileList.size) {
            fileList.removeAt(position)
            if (selectedPosition == position) {
                selectedPosition = -1
            } else if (selectedPosition > position) {
                selectedPosition--
            }
            notifyItemRemoved(position)
        }
    }

    /**
     * 根据文件名删除文件
     */
    fun removeFileByName(fileName: Item) {
        val position = fileList.indexOf(fileName)
        if (position != -1) {
            removeFile(position)
        }
    }

    inner class LocalPlayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvFileName: TextView = itemView.findViewById(R.id.tv_file_name)
        private val ivPreview: ImageView = itemView.findViewById(R.id.iv_preview)
        private val ivDelete: ImageView = itemView.findViewById(R.id.iv_delete)

        fun bind(file: Item, isSelected: Boolean) {
            tvFileName.text = file.name

            // 设置选中状态
            if (isSelected) {
                itemView.setBackgroundResource(R.drawable.selected_item_bg)
                tvFileName.setTextColor(itemView.context.getColor(R.color.blue_primary))
            } else {
                itemView.setBackgroundResource(R.drawable.normal_item_bg)
                tvFileName.setTextColor(itemView.context.getColor(R.color.black))
            }

            // 设置点击事件
            itemView.setOnClickListener {
                onItemAction(file, "select")
            }

            ivPreview.setOnClickListener {
                onItemAction(file, "preview")
            }

            ivDelete.setOnClickListener {
                onItemAction(file, "delete")
            }
        }
    }
}

